package org.canaan.http.urlpair.resolver.impl;


import org.canaan.http.urlpair.core.Utils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

class ResolveUtils {

    /**
     * 深入解析复杂对象
     *
     * @param key
     * @param obj
     * @param pairMap 最后解析完后，以键值对保存
     */
    public static void resolveObject(final String key, final Object obj, final Map<String, Object> pairMap) {
        if (obj == null) {
            return;
        }

        if (Collections.class.isInstance(obj)) {
            resolveCollection(key, obj, pairMap);
            return;
        } else if (obj.getClass().isArray()) {
            resolveArray(key, obj, pairMap);
            return;
        } else if (!Pattern.matches("(.*\\[\\d*\\])$", key)) {  //如果上次解析的为一个非集合对象，这此次又是一个非集合对象，不进行解析
            return;
        } else if (Map.class.isInstance(obj)) {
            resolveMap(key, obj, pairMap);
            return;
        }

        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            Object value;
            try {
                value = field.get(obj);
                if (value == null) {
                    continue;
                }
            } catch (IllegalAccessException e) {
                continue;
            }
            String _key = key + "[" + field.getName() + "]";

            if (Utils.isBaseType(value)) {
                pairMap.put(_key, Utils.urlEncoder(value));
            } else if (Collection.class.isInstance(value)) {
                resolveCollection(_key, value, pairMap);
            } else if (value.getClass().isArray()) {
                resolveArray(_key, value, pairMap);
            }
        }

    }


    /**
     * 解析map
     *
     * @param key
     * @param obj
     * @param pairMap
     */
    private static void resolveMap(final String key, final Object obj, final Map<String, Object> pairMap) {
        if (obj == null) {
            return;
        }
        Map<String, ?> map = (Map<String, ?>) obj;
        if (map.size() == 0) {
            return;
        }
        //验证map 的key 是不是一个String类型
        try {
            for (String itme : map.keySet()) {
                break;
            }
        } catch (Exception e) {
            return;
        }
        for (String itme : map.keySet()) {
            String _key = key + "[" + itme + "]";
            Object value = map.get(itme);
            if (value != null && Utils.isBaseType(value)) {
                pairMap.put(_key, Utils.urlEncoder(value));
            } else {
                resolveObject(_key, value, pairMap);
            }
        }

    }

    /**
     * 解析集合类型
     *
     * @param key
     * @param obj
     * @param pairMap
     */
    private static void resolveCollection(final String key, final Object obj, final Map<String, Object> pairMap) {
        if (obj == null) {
            return;
        }
        Collection<?> collection = (Collection<?>) obj;

        if (collection.size() == 0) {
            return;
        }

        int index = 0;
        for (Object item : collection) {
            String _key = key + "[" + index + "]";
            resolveObject(_key, item, pairMap);
            index++;
        }

    }

    /**
     * 解析数组类型
     *
     * @param key
     * @param obj
     * @param pairMap
     */
    private static void resolveArray(final String key, final Object obj, final Map<String, Object> pairMap) {
        Object[] arr = (Object[]) obj;
        if (arr.length == 0) {
            return;
        }

        int index = 0;
        for (Object item : arr) {
            String _key = key + "[" + index + "]";
            resolveObject(_key, item, pairMap);
            index++;
        }
    }

}
